package com.ehotting.edsta.mqbd.monitor.task.job;

import com.ehotting.eaf.core.DateHelper;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.edsta.mqbd.signal.api.bean.MqSignalTcpData;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.MqActionConstants;
import com.ehotting.mqbd.api.MqDataStateConstants;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.zw.api.bean.ZwMQTcpData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Collection;
import java.util.Date;

public class TcpChannelCheckTask {

    private Logger logger = LoggerFactory.getLogger(TcpChannelCheckTask.class);

    private RedisService redisService;

    private AgentLinkManager agentLinkManager;

    private MQService mqService;

    //@Scheduled(initialDelay=10000, fixedRate=10000)
    public void invoke(){
        logger.info("Start TcpChannelCheckTask");

        boolean lock = redisService.checkLock(MQDBConstants.REDIS_LOCK_KEY_TCP_CHANNEL, DateHelper.toString(new Date(),DateHelper.STD_FORMAT),1000L*30);
        if(lock == false){
            logger.info("TcpChannelCheckTask is locked,return.");
            return;
        }
        logger.info("Start active action");

        Collection<String> keys = redisService.keys(MQDBConstants.AGENT_REF_SOLVER_CHANNEL + "*");

        for(String key :keys){
            logger.info("key:{}",key);
            String agentId = redisService.get(key);
            String[] keyArray = key.split(MQDBConstants.COLO);
            /**MQDB:SERVER_INFO_KEY:{module}**/
            String module = keyArray[2];
            String channelId = keyArray[3];
            String state = checkAll( module, agentId,channelId);
            if(MqDataStateConstants.CHANNEL_INACTIVE.equals(state)){
                redisService.delete(key);
            }
        }

        logger.info("Finish TcpChannelCheckTask");
    }

    private String checkAll(String module,String agentId,String channelId){
        String consumerIp = agentLinkManager.getConsumerIpAndAgentId(module,agentId);
        String state = MqDataStateConstants.SUCCESS;
        if(SysModuleEnum.SIGNAL_ANALYSIS.getCode().equals(module)){
            state = checkSI(module, consumerIp , channelId);
        }

        if(SysModuleEnum.SIMULATION.getCode().equals(module)){
            state = checkZW( module, consumerIp , channelId);
        }

        return state;
    }

    private String checkSI(String module,String consumerIp,String channelId){
        MqApplicationContext mqApplicationContext = new MqApplicationContext(module,MQDBConstants.MONITOR_HOST,consumerIp);
        String testAgentId = agentLinkManager.genAgentId(module,0L,"0");
        MqSignalTcpData data = MqSignalTcpData.create(TopicEnum.CONCURRENCY,
                mqApplicationContext,
                testAgentId,
                channelId);
        data.setAction(MqActionConstants.CHECK_CHANNEL);
        MqSignalTcpData mqSignalTcpData = (MqSignalTcpData)mqService.request(data,60);
        return mqSignalTcpData.getState();
    }


    private String checkZW(String module,String consumerIp,String channelId){
        MqApplicationContext mqApplicationContext = new MqApplicationContext(module,MQDBConstants.MONITOR_HOST,consumerIp);
        String testAgentId = agentLinkManager.genAgentId(module,0L,"0");
        ZwMQTcpData data = ZwMQTcpData.create(TopicEnum.CONCURRENCY,
                mqApplicationContext,
                testAgentId,
                channelId);
        data.setAction(MqActionConstants.CHECK_CHANNEL);
        ZwMQTcpData zwMQTcpData = (ZwMQTcpData)mqService.request(data,60);
        return zwMQTcpData.getState();
    }

    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    public void setAgentLinkManager(AgentLinkManager agentLinkManager) {
        this.agentLinkManager = agentLinkManager;
    }

    public void setMqService(MQService mqService) {
        this.mqService = mqService;
    }

}
